<?php
/**
 * 日志查询类
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/20
 * Time: 16:17
 */
namespace app\common\controller;
use think\Db;

class LogsCm
{

    /**
     * 记录系统调试日志
     * @param $name
     * @param $title
     * @param $note
     */
    public static function WriteSystemLog($name, $title, $note)
    {
        $names = explode('/', $name);
        $fileName = $names[count($names) - 1];
        $fileName = $fileName . '_log.txt';
        unset($names[count($names) - 1]);
        mkdirs('../system_logs');
        $path = 'system_logs/' . date('Y-m-d');
        if (count($names) > 0) {
            foreach ($names as $name) {
                $path = $path . '/' . $name;
            }
        }

        if (!is_dir($path)) {
            $flag = mkdir($path, 0777, true);
        }
        file_put_contents($path . '/' . $fileName, date("Y-m-d H:i:s") . ' :' . $title . ':' . $note . " \n", FILE_APPEND);
    }



    /**
     * 获取后台操作日志
     * @param $username
     * @param $search_date
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function GetAdmLog($username, $search_date)
    {
        $start_time = $search_date . ' 00:00:00';
        $end_time = $search_date . ' 23:59:59';
        $cdn['add_time'] = array(array('egt', $start_time), array('elt', $end_time), 'AND');
        $cdn['username'] = $username;
        $rows = Db::name('log_op_manager')->where($cdn)->select();
        return $rows;
    }

    /**
     * 写入管理员日志
     * @param $username [管理员账号]
     * @param $content [日志内容]
     */
    public static function WriteAdmLog($username, $content)
    {
        $add_time = date('Y-m-d H:i:s');
        $ip = CommonFun::get_ip();
        $op_url = $_SERVER['HTTP_HOST'];
        $data = ['username' => $username, 'content' => $content, 'add_time' => $add_time, 'ip' => $ip, 'url' => $op_url];
        Db::name('log_op_manager')->insert($data);
    }


}